import http from '@/utils/request.js'
import API from '@/config/api.config'

Page({
  data: {
    userInfo: {}
  },

  onShow() {
    const app = getApp();
    app.checkToken().then(valid => {
      console.log("usercenter.js onShow() token验证结果: "+valid);
    });
  },
  
  onLogout() {
    const app = getApp();
    app.globalData.recorderManager.stop();
    http.request({
      url: API.login.logout,
      method: 'POST',
      data: {
        'X-Refresh-Token': "Bearer" + wx.getStorageSync('refresh_token')
      }
    }).then(res => {
      wx.removeStorageSync('access_token')
      wx.removeStorageSync('refresh_token')
      wx.showToast({
        title: '退出成功',
        icon: 'success',
        duration: 2000
      });
      wx.reLaunch({
        url: '/pages/login/login'
      })
    })
  },
  onLoad() {
    const app=getApp()
    wx.setKeepScreenOn({
      keepScreenOn: app.globalData.isRecording,
      success: () => console.log('常亮已启用')
    });
    // 修复tabbar切换时页面栈的问题
    if (getCurrentPages().length === 1) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
  },
  onUnload() {
    const app=getApp()
    wx.setKeepScreenOn({
      keepScreenOn: app.globalData.isRecording,
      success: () => console.log('常亮已启用')
    });
  }
})
